package com.hshx.controller;

import com.github.pagehelper.Page;
import com.hshx.client.UsersClient;
import com.hshx.pojo.Salary;
import com.hshx.service.GrantService;
import com.hshx.service.SalaryService;
import com.hshx.util.RedisUtil1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@CrossOrigin
@RequestMapping("/salaryService")
@RestController
public class SalaryController {
    @Autowired
    private SalaryService salaryService;
    @Autowired
    private UsersClient usersClient;
    @Autowired
    private RedisUtil1 redisUtil1;
    /**
     *添加薪酬标准
     * @param salary
     * @return
     */
    @RequestMapping("/addStandardCriteria")
    public boolean addStandardCriteria(Salary salary){
        if (salaryService.addStandardCriteria(salary)>0){
            return true;
        }
        return false;
    }


    /**
     * 根据id删除
     * @param salary_id
     * @return
     */
    @RequestMapping("/deleteStandardCriteria")
    public boolean deleteStandardCriteria(Integer salary_id){
        if (salaryService.deleteStandardCriteria(salary_id)>0){
            return true;
        }
        return false;
    }

    /**
     * 根据id集合批量删除
     * @param salary_id_list
     * @return
     */
    @RequestMapping("/deleteStandardCriteriaList")
    public boolean deleteStandardCriteriaList(String salary_id_list){
        if (salaryService.deleteStandardCriteriaList(Arrays.asList(salary_id_list.split(",")))>0){
            return true;
        }
        return false;
    }
    /**
     * 查询所有薪酬标准
     */
    @RequestMapping("/queryStandardCriteria")
    public HashMap<String,Object> queryStandardCriteria(){
//        System.out.println("queryStandardCriteria");
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("queryStandardCriteriaAll",salaryService.queryStandardCriteria());
        return hashMap;
    }

    /**
     * 根据id查询标准
     * @param salary_id
     * @return
     */
    @RequestMapping("/salaryID")
    public Salary salaryID(Integer salary_id){
        System.out.println("sdsdsdssdsdsdsdsdsssssssssssssssssssssssssssssssssssssss:"+salaryService.querySalaryID(salary_id));
        Salary salary = salaryService.querySalaryID(salary_id);
        return salary;
    }
    /**
     * 失去焦点查询标准编号是否存在
     * @param salary_no
     * @return
     */
    @RequestMapping("/blurSalaryNo")
    public boolean blurSalaryNo(String salary_no){
        if (salaryService.querySalaryNo(salary_no).size()>0){
            return false;
        }
        return true;
    }

    /**
     * 根据薪酬ID判断标准是否被使用
     * @param salary_id
     * @return
     */
    @RequestMapping("/blurSalaryIDUsersID")
    public boolean blurSalaryIDUsersID(Integer salary_id){
        return usersClient.hasSalary(salary_id);
    }



    /**
     * 失去焦点查询标准名是否存在
     * @param salary_name
     * @return
     */
    @RequestMapping("/blurSalaryName")
    public boolean blurSalaryName(String salary_name){
        if (salaryService.querySalaryName(salary_name).size()>0){
            return false;
        }
        return true;
    }
    /**
     * 薪酬标准搜索
     * @param page
     * @param limit
     * @param salary_date
     * @param salary_no
     * @param salary_name
     * @return
     */
    @RequestMapping("findCompensationCriteriaSearch")
    public HashMap<String,Object> findCompensationCriteriaSearch(int page,int limit,String salary_date,String salary_no,String salary_name){
        HashMap<String,Object> hashMap = new HashMap<>();
        System.out.println("pageIndex:"+page+"pageSize:"+limit+"date:"+salary_date+"no:"+salary_no+"name:"+salary_name);
        Page<Salary> data = salaryService.findCompensationCriteriaSearch(page, limit, salary_date, salary_no, salary_name);
        hashMap.put("count",data.getTotal());
        hashMap.put("data",data);
        hashMap.put("code",0);
        return hashMap;
    }

    /**
     * 分页显示薪酬标准
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findPagination")
    public HashMap<String,Object> findPagination(int page,int limit){
        HashMap<String,Object> hashMap = new HashMap<>();
        System.out.println("pageIndex:"+page+"pageSize:"+limit);
        Page<Salary> data = salaryService.findPagination(page,limit);
//        System.out.println("Total:"+page.getTotal());
        hashMap.put("count",data.getTotal());
        hashMap.put("data",data);
        hashMap.put("code",0);
        return hashMap;
    }

    /**
     * 修改薪酬标准
     * @param salary
     * @return
     */
    @RequestMapping("/updateStandardCriteria")
    public boolean updateStandardCriteria(Salary salary){
        if (salaryService.updateStandardCriteria(salary)>0){
            return true;
        }
        return false;
    }
    /**
     * 根据id修改标准状态
     * @return
     */
    @RequestMapping("/updateSalaryState")
    public boolean updateSalaryState(Integer salary_id,Integer salary_state){
        if (salaryService.updateSalaryState(salary_id, salary_state)>0){
            return true;
        }
        return false;
    }
//    @Resource
//    private GrantService grantService;
//    @RequestMapping("/getSalaryVo")
//    public HashMap<String,Object> getSalaryVo(int page,int limit){
//        return salaryService.getSalaryVo(page, limit);
//    }

    @RequestMapping("/standardAnalysis")
    public HashMap<String,Object> standardAnalyse1(){
        HashMap<String,Object> hashMap = new HashMap<>();
        List<String> integerList = new ArrayList<>();
        for (int i=0; i<5 ;i++){
            integerList.add(findYearMonth(i));
            hashMap.put("monthDate"+i,salaryService.standardAnalyse1(integerList.get(i)));
        }
        hashMap.put("month",integerList);
        return hashMap;
    }
    /**
     * 获取年月
     * @return
     */
        private static String findYearMonth(int mon){
        /**
         * 实例化一个对象calendar
         */
        Calendar calendar = Calendar.getInstance();
        /**
         * 获取年份
         */
        int year = calendar.get(Calendar.YEAR);
        /**
         * 获取月份
         */
        int month = calendar.get(Calendar.MONTH) + 1-mon;
        if (month==0||month<0){
            year=year-1;
            month=month+12;
        }
        /**
         * 拼接年份和月份
         */
        String date = year + "-" + ( month<10 ? "0" + month : month);
        /**
         * 返回当前年月
         */
        return date;
    }
}
